﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartHotel.Dto;
using SmartHotel.HousingPrice;
using SmartHotel.HousingPrice.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SmartHotel.HousingPrice
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppHousingPriceService : ApplicationService,ISmartHotelAppHousingPriceService
    {
       public  SmartHotelAppHousingPriceService(IRepository<HousingPriceTable,int> HousingPrice)
        {
            this.HousingPrice = HousingPrice;
        }

        public IRepository<HousingPriceTable, int> HousingPrice { get; }
        /// <summary>
        /// 添加房价
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateHousingPriceDto>> CreateHousingPriceAsync(CreateandUpdateHousingPriceDto dto)
        {
            var list = ObjectMapper.Map<CreateandUpdateHousingPriceDto, HousingPriceTable>(dto);
            await HousingPrice.InsertAsync(list);
            return new ResultDto<CreateandUpdateHousingPriceDto> { Code=Result.Success, Data = list,Message="添加成功" };
        }
        /// <summary>
        /// 删除房价
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<bool>> DeleteHousingPriceAsync(int Id)
        {
            await HousingPrice.DeleteAsync(Id);
            return new ResultDto<bool> { Code = Result.Success,Message="删除成功" };
        }
        /// <summary>
        /// 反填房价
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<HousingPriceDto>> GetFindHousingPriceAsync(int Id)
        {
            var list= await HousingPrice.GetAsync(Id);
            return new ResultDto<HousingPriceDto> { Code = Result.Success, Data = list, Message = "反填成功" };
        }
        /// <summary>
        /// 显示房价
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<List<HousingPriceDto>>> GetAllHousingPricesAsync()
        {
            var list=await HousingPrice.GetListAsync();
            return new ResultDto<List<HousingPriceDto>> { Code = Result.Success, Data = list, Message = "显示成功" };
        }
        /// <summary>
        /// 根据Id查询房价
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDto<List<HousingPriceDto>>> GetIdHousingPricesAsync(int id)
        {
            var list = await HousingPrice.GetListAsync();
            if (id > 0)
            {
                list=  list.Where(x=>x.Id==id).ToList();
            }
            return new ResultDto<List<HousingPriceDto>> { Code = Result.Success, Data = list, Message = "显示成功" };
        }
        /// <summary>
        ///分页显示房价
        /// </summary>
        /// <param name="pgaeIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="ProductName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageHousingPriceDto<List<HousingPriceDto>>> GetPageHousingPricesAsync(int pageIndex, int pageSize, string ProductName)
        {
            var totalCount=await HousingPrice.GetCountAsync();
            var list=await HousingPrice.GetQueryableAsync();
            if (!string.IsNullOrEmpty(ProductName))
            {
                list = list.WhereIf(!string.IsNullOrEmpty(ProductName), x => x.ProductName.Contains(ProductName));
            }
            var housingPrices=await list.OrderBy(x=>x.Id).Skip((pageIndex-1)*pageSize).Take(pageSize).ToListAsync();
            var dto = ObjectMapper.Map< List<HousingPriceTable>,List<HousingPriceDto>>(housingPrices);
            return new PageHousingPriceDto<List<HousingPriceDto>>{ Code = Result.Success, Data = dto, Message = "分页显示成功",totalCount=totalCount };

        }
        /// <summary>
        /// 修改房价
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateHousingPriceDto>> UpdateHousingPriceAsync(CreateandUpdateHousingPriceDto dto)
        {
            var list =await HousingPrice.GetAsync(dto.Id);
             list =  ObjectMapper.Map(dto,list);
           await HousingPrice.UpdateAsync(list);
            return new ResultDto<CreateandUpdateHousingPriceDto> { Code = Result.Success, Message = "修改成功" };
        }
    }
}
